<?php

namespace app\merchants\services;

use app\common\loader\ModelMerchant;
use app\common\loader\ModelVirtual;
use app\common\loader\Services;
use think\db\exception\DbException;
use think\Exception;

/**
 * 虚码审核管理（日志）
 */
class VirtualUsedLogService
{

    /**
     * 虚码使用记录
     * @param string $merId
     * @param array $option
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function virtualUsedLog(string $merId, array $option)
    {

        $pageList = ModelVirtual::VirtualUsedLog()->pageList($merId, $option);
        if (!empty($pageList['data'])) {
            $ServiceHelper = Services::Helper();
            $ServiceHelper->showListTitle($pageList['data'],
                [
                    'salesman' => 'salesman_id',
                    'merchant' => 'admin_id'
                ]
            );

            // 获
            $log_status = config('system.used_log_status');
            foreach ($pageList['data'] as &$row) {
                $row['product_tag'] = '';
                if (!empty($row['product'])) {
                    $result = array_map(function ($v) {
                        return $v['product_title'] . ' * ' . $v['num'];
                    }, $row['product']);

                    $row['product_tag'] = implode('; ', $result);
                }

                $row['mer_title'] = $row['admin_tag'];
                unset($row['product']);
                $row['ticket_url'] = explode(',', $row['ticket_url']);
                $row['update_at'] = empty($row['update_at']) ? $row['create_at'] : $row['update_at'];
                // 审核成功
                $row['status_tag'] = $log_status[$row['status']];
            }
        }
        return $pageList;
    }

    /**
     * 虚码审核管理列表
     * @param array $where
     * @param  $mer_id
     * @return array
     * @throws Exception
     */
    public function getPage($mer_id, array $where = [], $page = 1, $page_size = 30): array
    {
        //        if (!empty($data['data'])){
//           $shop_salesman = ModelShop::ShopSalesman()->where(['mer_id' => $mer_id])->column('name','id');
//            foreach ($data['data'] as $key => $v){
//                if (isset($shop_salesman[$v['salesman_id']])){
//                    $data['data'][$key]['salesman_name'] = $shop_salesman[$v['salesman_id']];
//                }else {
//                    $data['data'][$key]['salesman_name'] ='';
//                }
//            }
//        }
        return ModelVirtual::VirtualUsedLog()->getMarket($where , $page, $page_size);
    }

    /**
     * 虚码审核管理列表
     * @param $mer_id
     * @param $id
     * @return array
     * @throws DbException
     */
    public function getInfo($mer_id, $id): array
    {
        $where = ['mer_id' => $mer_id , 'id' => $id];
        return ModelVirtual::VirtualUsedLog()->getInfo($where);
    }


    /**
     * 虚码审核
     * @param $mer_id
     * @param $id
     * @param $status
     * @param $admin_id
     * @param string $memo
     * @return bool
     * @throws Exception
     */
    public function submitAudit($mer_id, $id , $status,$admin_id , $memo = ''): bool
    {
        $where = ['mer_id' => $mer_id , 'id' => $id];
       return  ModelVirtual::VirtualUsedLog()->submitAudit($where,$status,$admin_id ,$memo);

    }

    /**
     * 虚码列表
     * @param array $where
     * @param int $limit
     * @return array
     * @throws DbException
     */
    public function getList(array $where = [], int $limit = 100): array
    {
        return ModelVirtual::VirtualUsedLog()->getList($where, $limit);

    }


    /**
     * 支付红包记录
     * @param string $merId 商户号
     * @param array $ids 记录ID
     * @return integer
     */
    public function payRedPacket(string $merId, array $ids)
    {
        return ModelMerchant::PendingRedPacket()->payRedPacket($merId, $ids);
    }
}
